package com.studyv3.test.controller.productsbuy;

import com.studyv3.common.core.controller.BaseController;
import com.studyv3.common.core.domain.AjaxResult;
import com.studyv3.common.core.domain.R;
import com.studyv3.common.core.page.TableDataInfo;
import com.studyv3.test.domain.ApplyProduct;
import com.studyv3.test.domain.Products;
import com.studyv3.test.domain.Stock;
import com.studyv3.test.service.ApplyApprovalService;
import com.studyv3.test.service.ApplyProductService;
import com.studyv3.test.service.IStockService;
import com.studyv3.test.service.ProductsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/warehous/")
public class WarehousController extends BaseController {
    @Autowired
    ApplyProductService applyProductService;
    @Autowired
    ApplyApprovalService applyApprovalService;
    @Autowired
    ProductsService productsService;
@Autowired
    IStockService stockService;

    @PostMapping("agree")
    public AjaxResult agree(@RequestBody ApplyProduct applyProduct) {

        applyProduct.setFlag(1);
        applyApprovalService.agreeOrReject(applyProduct);

        return applyApprovalService.agreeOrReject(applyProduct);
    }

    @PostMapping("reject")
    public AjaxResult reject(@RequestBody ApplyProduct applyProduct) {

        applyProduct.setFlag(0);

        return applyApprovalService.agreeOrReject(applyProduct);
    }

    /*
    * 我的已审
    * */
    @GetMapping("myapproval")
    public TableDataInfo myapproval(ApplyProduct applyProduct) {
        applyProduct.setApprovalUser(getUserId().intValue());
        applyProduct.setFlag(2);
        applyProduct.setDeleted(0);
        startPage();
        List<ApplyProduct> list = applyProductService.selectApplyByMeProductList(applyProduct);
        return getDataTable(list);
    }

    /*
    * 入库详情
    * */
    @PostMapping("todowarehous")
    public R warehoused(@RequestBody ApplyProduct applyProduct)  {
        applyProduct.setFlag(1);
        applyProduct.setDeleted(0);
        R r = new R();
        r.setData(applyProductService.selectApplyProductList(applyProduct));
        return r;
    }

    @Transactional
    @PostMapping("warehous")
    public R warehous(@RequestBody ApplyProduct applyProduct1){
        AjaxResult ajaxResult = new AjaxResult();
        ApplyProduct applyProduct = applyProductService.selectApplyProductById(applyProduct1.getId());
        applyProduct.setWarehousFlag(1);
        applyProduct.setWarehousTime(LocalDateTime.now());
        ajaxResult=applyProductService.warehousApplyProduct(applyProduct);
        if(ajaxResult.isError()){
            return R.fail();
        }
        Products products = productsService.productsById(applyProduct.getProductsId());
        Stock stock = new Stock();
        stock.setBatchId(applyProduct.getBatchId());
        stock.setProductsId(applyProduct.getProductsId());
        if(stock.getProductsNum()==null){
            stock.setProductsNum(applyProduct.getAmount().longValue());
        }else{
        stock.setProductsNum(stock.getProductsNum()+applyProduct.getAmount());}
        stock.setWarehousDate(LocalDateTime.now());
//        stock.setProductsApply(applyProduct.getId().longValue());
        stock.setEndDate(LocalDate.now().plusDays(products.getValidDays()));
        stockService.save(stock);
        return R.ok();
    }



}
